﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace FYPDemo
{
    public partial class Comment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string Title;
            Title = Session["Title"].ToString(); 
            GetComment(Title);

            if (Session["msg"] != null)
            {
                string msg = Session["msg"].ToString();
                MessageBox.Show(msg);
                Session["msg"] = null;
            }

            if (!IsPostBack)
            {
                if (Session["username"] != null)
                {
                    btnAddComment.Enabled = true;
                }
            }
        }

        protected void CommentGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.CommentGrid.PageIndex = e.NewPageIndex;
            GetComment((Session["Title"].ToString()));
        }

        protected void GetComment(string title)
        {
            DataTable dt = VirtualCommunication.listComment(title);

            CommentGrid.DataSource = dt;
            CommentGrid.DataBind();
        }

        protected void Comment_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                int id = Convert.ToInt32(CommentGrid.DataKeys[index].Values["ID"]);

                int result = DeleteComment(id);

                if (result > 0)
                {
                    Response.Redirect(Request.RawUrl);
                }

                else
                {
                    Session["msg"] = "You have no authority to delete the comment.";
                    Response.Redirect(Request.RawUrl);
                }
            }

            if (e.CommandName == "Reply")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                int id = Convert.ToInt32(CommentGrid.DataKeys[index].Values["ID"]);

                DataTable dt = VirtualCommunication.getCommentByID(id);

                string text = "“" +dt.Rows[0][0].ToString()+ "/nBy " +dt.Rows[0][1].ToString()+ "”";
                string newText = text.Replace("/n", System.Environment.NewLine);
                txtAddComment.Text = newText;
                txtAddComment.Focus(); 
            }
        }

        protected void btnAddComment_Click(object sender, EventArgs e)
        {
            if (Session["username"] != null)
            {
                int result = 0;

                string comment = txtAddComment.Text;

                if (comment != "")
                {
                    string title = Session["Title"].ToString();
                    string username = Session["username"].ToString();

                    result = VirtualCommunication.addComment(title, comment, username, username);

                    if (result > 0)
                    {
                        Response.Redirect(Request.RawUrl);
                    }
                }
            }            
        }

        public int DeleteComment(int ID)
        {
            int result = 0;

            if (Session["username"] != null)
            {
                string user = Session["username"].ToString();

                result = VirtualCommunication.deleteComment(ID, user);
            }

            return result;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Forum.aspx");
        }
    }
}